/*
 * PanelCekKonfirmasi.java
 *
 * Created on 18 Desember 2011, 20:33
 */
package Boundary;

import Controller.DatabaseHandler;
import Controller.dataBasePesan;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author  Jeanot Nahasan
 */
public class PanelCekKonfirmasi extends javax.swing.JPanel {

    /** Creates new form PanelCekKonfirmasi */
    public PanelCekKonfirmasi() {
        initComponents();
    }

    PanelCekKonfirmasi(JPanel panelActive) {
        initComponents();
        panelPenerimaPesan.setVisible(false);

    }

    public void showTabelPesan() {
        try {

            String query = "select * from pesan order by nomor_pesan desc";
            PreparedStatement psmt = DatabaseHandler.getDBConnection().prepareStatement(query);


            ResultSet rs = psmt.executeQuery();
            LinkedList x = new LinkedList();
            while (rs.next()) {
                x.add(rs.getString("nomor_pesan"));
                x.add(rs.getString("judul_pesan"));
                x.add(rs.getString("isi_pesan"));
                x.add(rs.getString("tanggal_pesan"));
            }

            DefaultTableModel df2 = new DefaultTableModel();
            String[] identifier = {"No Pesan", "Judul Pesan", "Isi Pesan", "Tanggal Kirim"};
            df2.setColumnIdentifiers(identifier);


            while (!x.isEmpty()) {
                String[] data = new String[4];
                for (int i = 0; i < data.length; i++) {
                    data[i] = String.valueOf(x.remove());
                }
                df2.addRow(data);
            }
            tabelPesan.setModel(df2);

        } catch (SQLException ex) {
            Logger.getLogger(PanelCekKonfirmasi.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void showTabelPenerimaPesan() {
        try {
            int posR = tabelPesan.getSelectedRow();
            String confirmStatus = "none";

            int noPesan = Integer.parseInt(tabelPesan.getValueAt(posR, 0).toString().trim());

            String query = "select no_hp from pesan_terkirim where nomor_pesan='" + noPesan + "'";
            PreparedStatement psmt1 = DatabaseHandler.getDBConnection().prepareStatement(query);
            ResultSet rs1 = psmt1.executeQuery();
            int no = 1;

            LinkedList x = new LinkedList();
            while (rs1.next()) {
                String noHP = rs1.getString("no_hp");
                String queryAmbilDataMember = "select namamember, nohpmember from member where nohpmember ='" + noHP + "'";
                PreparedStatement psmt = DatabaseHandler.getDBConnection().prepareStatement(queryAmbilDataMember);
                ResultSet rs = psmt.executeQuery();

                while (rs.next()) {
                    x.add(no);
                    x.add(rs.getString("namamember"));
                    x.add(rs.getString("nohpmember"));
                    x.add(confirmStatus);
                    no++;
                }
            }

            DefaultTableModel df2 = new DefaultTableModel();
            String[] identifier = {"No", "Nama", "No Hp", "Confirm Status"};
            df2.setColumnIdentifiers(identifier);

            tabelPenerimaPesan.getColumnModel().getColumn(0).setResizable(false);
            while (!x.isEmpty()) {
                String[] data = new String[4];
                for (int i = 0; i < data.length; i++) {
                    data[i] = String.valueOf(x.remove());
                }
                df2.addRow(data);
            }
            tabelPenerimaPesan.setModel(df2);

        } catch (SQLException ex) {
            Logger.getLogger(PanelCekKonfirmasi.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnLihatPenerima = new javax.swing.JButton();
        btnTambah = new javax.swing.JButton();
        btnHapus = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelPesan = new javax.swing.JTable();
        panelPenerimaPesan = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tabelPenerimaPesan = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();

        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnLihatPenerima.setText("Lihat Penerima Pesan");
        btnLihatPenerima.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLihatPenerimaActionPerformed(evt);
            }
        });
        add(btnLihatPenerima, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 140, 140, -1));

        btnTambah.setText("Pesan Baru");
        btnTambah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTambahActionPerformed(evt);
            }
        });
        add(btnTambah, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 140, 110, -1));

        btnHapus.setText("Hapus");
        btnHapus.setToolTipText("Klik untuk menghapus member");
        btnHapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHapusActionPerformed(evt);
            }
        });
        add(btnHapus, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 140, 70, -1));

        tabelPesan.setAutoCreateRowSorter(true);
        tabelPesan.setBackground(new java.awt.Color(204, 204, 204));
        tabelPesan.setFont(new java.awt.Font("Tahoma", 0, 12));
        tabelPesan.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "No Pesan", "Judul Pesan", "Isi Pesan", "Tanggal Kirim"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabelPesan.setGridColor(new java.awt.Color(0, 153, 204));
        tabelPesan.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(tabelPesan);
        tabelPesan.getColumnModel().getColumn(0).setResizable(false);
        tabelPesan.getColumnModel().getColumn(0).setPreferredWidth(30);
        tabelPesan.getColumnModel().getColumn(1).setResizable(false);
        tabelPesan.getColumnModel().getColumn(1).setPreferredWidth(60);
        tabelPesan.getColumnModel().getColumn(3).setResizable(false);

        add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 170, 900, 140));

        panelPenerimaPesan.setBackground(new java.awt.Color(0, 153, 255));
        panelPenerimaPesan.setVisible(false);
        panelPenerimaPesan.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Penerima Pesan", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14), new java.awt.Color(255, 255, 255)));

        tabelPenerimaPesan.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "No", "No Hp"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabelPenerimaPesan.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelPenerimaPesanMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                tabelPenerimaPesanMouseExited(evt);
            }
        });
        jScrollPane2.setViewportView(tabelPenerimaPesan);
        tabelPenerimaPesan.getColumnModel().getColumn(0).setResizable(false);
        tabelPenerimaPesan.getColumnModel().getColumn(0).setPreferredWidth(30);
        tabelPenerimaPesan.getColumnModel().getColumn(1).setResizable(false);

        javax.swing.GroupLayout panelPenerimaPesanLayout = new javax.swing.GroupLayout(panelPenerimaPesan);
        panelPenerimaPesan.setLayout(panelPenerimaPesanLayout);
        panelPenerimaPesanLayout.setHorizontalGroup(
            panelPenerimaPesanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelPenerimaPesanLayout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(414, Short.MAX_VALUE))
        );
        panelPenerimaPesanLayout.setVerticalGroup(
            panelPenerimaPesanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelPenerimaPesanLayout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        add(panelPenerimaPesan, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 320, 900, 180));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gambar/bg.jpg"))); // NOI18N
        add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 960, 540));
    }// </editor-fold>//GEN-END:initComponents
    private void btnHapusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHapusActionPerformed
        dataBasePesan pesan = new dataBasePesan();
        int posR = tabelPesan.getSelectedRow();
        String nomorPesan = null;
        nomorPesan = tabelPesan.getValueAt(posR, 0).toString().trim();
        pesan.hapusPesan(nomorPesan);
        showTabelPesan();
        JOptionPane.showMessageDialog(null, "pesan sudah dihapus");       
    }//GEN-LAST:event_btnHapusActionPerformed

    private void btnTambahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTambahActionPerformed
        Home.tampilFramePesan();
    // TODO add your handling code here:
    }//GEN-LAST:event_btnTambahActionPerformed

    private void btnLihatPenerimaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLihatPenerimaActionPerformed
        panelPenerimaPesan.setVisible(true);
        showTabelPenerimaPesan();// TODO add your handling code here:
}//GEN-LAST:event_btnLihatPenerimaActionPerformed

    private void tabelPenerimaPesanMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelPenerimaPesanMouseClicked
    // TODO add your handling code here:
}//GEN-LAST:event_tabelPenerimaPesanMouseClicked

    private void tabelPenerimaPesanMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelPenerimaPesanMouseExited
    // TODO add your handling code here:
}//GEN-LAST:event_tabelPenerimaPesanMouseExited

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnHapus;
    private javax.swing.JButton btnLihatPenerima;
    private javax.swing.JButton btnTambah;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPanel panelPenerimaPesan;
    private javax.swing.JTable tabelPenerimaPesan;
    private javax.swing.JTable tabelPesan;
    // End of variables declaration//GEN-END:variables
}
